/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.ms1_intensitiesretrieval;

import java.util.List;
import uk.ac.ebi.jmzml.model.mzml.BinaryDataArray;
import uk.ac.ebi.jmzml.model.mzml.Spectrum;

/**
 *
 * @author Jonas
 */
public class CompareWithMSLims {
    
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(CompareWithMSLims.class);

    public void checkWithMSLims(double spectrumRetentionTime, Spectrum currentSpectrum, List<MSLimsHits> identifications) {
        
        // Loop over the msLims data and check for a matching ms_lims retentiontime
        for (MSLimsHits identification : identifications) {
                        
            // Check if the recorded spectrum is in the window of the current ms_lims identification and if yes, get the peaks in the spectrum
            if (spectrumRetentionTime >= identification.getStartPoint() && spectrumRetentionTime <= identification.getEndPoint()) {

                // Make 2 binaryDataArray lists to hold the spectra m/z-values and the corresponding intensities
                List<BinaryDataArray> binaryDataArrayList = currentSpectrum.getBinaryDataArrayList().getBinaryDataArray();
                BinaryDataArray mzBinaryDataArray = (BinaryDataArray) binaryDataArrayList.get(0);
                // Make one array to hold mzValues and another one to hold intensities
                Number[] mzNumbers = mzBinaryDataArray.getBinaryDataAsNumberArray();
                BinaryDataArray intBinaryDataArray = (BinaryDataArray) binaryDataArrayList.get(1);
                Number[] intNumbers = intBinaryDataArray.getBinaryDataAsNumberArray();

                // Loop over the m/z values in that spectrum
                for (int i = 0; i < mzNumbers.length; i++) {
                    // Check if the spectrum holds the m/z-peak of the ms_limsidentification, 5ppm tolerance
                    if (Math.abs((mzNumbers[i].doubleValue() - identification.getPrecursorMZ()) / identification.getPrecursorMZ()) < 0.000005) {
                        // Set the highest peak
                        if (identification.getHighestPeak() < intNumbers[i].doubleValue()) {
                            identification.setHighestPeak(intNumbers[i].doubleValue());
                        }
                        identification.increaseSumOfPeaks(intNumbers[i].doubleValue());
                    }
                }
                
                LOGGER.info("Intensities set!");
            }
        }
    }
}
